<?php
class control_admin extends control {
	
	public function __construct($inPath)
	{
		parent::__construct();
	}
	
	public function pageindex($inPath)
	{
		$this->pagelogin($inPath);
	}
	
	
	/**
	 * 管理员登录
	 */
	public function pagelogin($inPath) {
		$action = $this->createUrl ( 'admin/login' );
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			if (isset ( $this->requests->post ['validnum'] ) && ! empty ( $this->requests->post ['validnum'] )) {
				$this->requests->post ['validnum'] = md5 ( strtolower ( $this->requests->post ['validnum'] ) );
				/**
				 * 验证码错误
				 */
				if ($this->requests->post ['validnum'] != $this->requests->cookie ['validString']) {
					exit ( json_encode ( array ('success' => 0, 'code' => 701, 'msg' => $this->languages ['login_validnum_error'] ) ) );
				}
				
				
				unset ( $this->requests->cookie ['validString'] );
				
				if (isset ( $this->requests->post ['username'] ) && isset ( $this->requests->post ['password'] )) {
					$list = $this->module_adminuser ()->selectColm ( array ('user' => $this->requests->post ['username'] ), array ('user', 'pass', 'ID', 'login_num' ) );
					/**
					 * 没有此用户
					 */
					if (empty ( $list )) {
						exit ( json_encode ( array ('success' => 0, 'code' => 702, 'msg' => $this->languages ['login_user_error'] ) ) );
					}
					/**
					 * 密码错误
					 */
					if ($list ['pass'] != md5 ( $this->requests->post ['password'] )) {
						exit ( json_encode ( array ('success' => 0, 'code' => 703, 'msg' => $this->languages ['login_pass_error'] ) ) );
					}
					$time = getConfig ( 'loginout' );
					$time = strtotime ( $time );
					$this->requests->set ( 'session', 'ADMIN_ID', $list ['ID'] );
					$this->requests->set ( 'session', 'LOGIN_TIME', time () );
					$this->requests->set ( 'session', 'LOGIN_OUT', $time );
					$this->requests->set ( 'session', 'ADMIN_NAME', $list ['user'] );
					$NewAry = array ('login_data' => date ( 'Y-m-d H:i:s' ), 'login_num' => $list ['login_num'] + 1, 'ID' => $list ['ID'] );
					
					/**
					 * 更新操作失败
					 */
					if ($this->module_adminuser ()->edit ( $NewAry ) === FALSE) {
						exit ( json_encode ( array ('success' => 0, 'code' => 704, 'msg' => $this->languages ['login_faile'] ) ) );
					}
					/**
					 * 登录成功
					 */
					exit ( json_encode ( array ('success' => 1, 'code' => 200, 'msg' => $this->languages ['login_success'] ) ) );
				}
			} else {
				exit ( json_encode ( array ('success' => 0, 'code' => 701, 'msg' => $this->languages ['login_validnum_error'] ) ) );
			}
		}
		$params['success_url']= $this->createUrl ( 'admin/home' );
		$params['action']= $action;
		$this->render ('admin/login.tpl',$params);
	}
	public function dohome() {
		$this->checklogin ();
		view::set ( 'language', $this->languages );
		view::tpl ( 'admin_home.php' );
		view::render ();
	}
	public function doinfo() {
		$this->checklogin ();
		view::set ( 'language', $this->languages );
		view::tpl ( 'admin_info.php' );
		view::render ();
	}
	public function donews() {
		$this->checklogin ();
		view::set ( 'language', $this->languages );
		view::tpl ( 'admin_news.php' );
		view::render ();
	}
	public function dolist() {
		$this->checklogin ();
		view::set ( 'language', $this->languages );
		view::tpl ( 'admin_list.php' );
		view::render ();
	}
	/**
	 * 验证码
	 */
	public function dologinvalid() {
		$width = isset ( $this->requests->get ['width'] ) ? intval ( $this->requests->get ['width'] ) : 60;
		$height = isset ( $this->requests->get ['height'] ) ? intval ( $this->requests->get ['height'] ) : 20;
		$len = isset ( $this->requests->get ['len'] ) ? intval ( $this->requests->get ['len'] ) : 4;
		$txtColor = isset ( $this->requests->get ['tc'] ) ? $this->requests->get ['tc'] : '#3e3e3e';
		$backColor = isset ( $this->requests->get ['bc'] ) ? $this->requests->get ['bc'] : '#FFFFFF';
		$image = new Image ();
		$valid = $image->imageValidate ( $width, $height, $len, 0, $txtColor, $backColor );
		$cookiename = isset ( $this->requests->get ['vname'] ) ? $this->requests->get ['vname'] : 'validString';
		setcookie ( $cookiename, md5 ( strtolower ( $valid ) ) );
		header ( "Pragma: no-cache" );
		header ( "Cache-control: no-cache" );
		$image->display ();
	}
	/**
	 * 
	 * 检查登录的有效性
	 */
	private function checklogin() {
		$time = time ();
		if (! isset ( $this->requests->session ['ADMIN_ID'] ) && empty ( $this->requests->session ['ADMIN_ID'] )) {
			session_destroy ();
			unset ( $_COOKIE );
			gourl ( get_url ( 'admin/login' ) );
		}
		if ($this->requests->session ['LOGIN_OUT'] < $time) {
			session_decode ();
			gourl ( get_url ( 'admin/login' ) );
		} else {
			$time = getConfig ( 'loginout' );
			$time = strtotime ( $time );
			$this->requests->set ( 'session', 'LOGIN_OUT', $time );
		}
	}
	/**
	 * 
	 * 登出
	 */
	public function dologout() {
		session_destroy ();
		unset ( $_COOKIE );
		gourl ( get_url ( 'admin/login' ) );
	}
	public function dotest() {
	}
}
?>